// 导入组件
import Vue from 'vue';
import Router from 'vue-router';
import {PcCookie, Key} from '@/utils/cookie';
import store from "@/store";
// 登录
import login from '@/views/login';
// 首页
import index from '@/views/index';
// 分类管理
import Category from '@/views/category';
// 标签管理
import Label from '@/views/label';
// 相册管理
import Picture from '@/views/picture/index';
import Detail from '@/views/picture/detail';
// 音乐列表
import Song from '@/views/music/song';
// 歌手列表
import Singer from '@/views/music/singer';
// 歌曲分类
import SongCategory from '@/views/music/song/category';
// MV分类
import MvCategory from '@/views/music/mv/category';
// 歌单列表
import SongList from '@/views/music/songList';
// 歌单分类
import SongListCategory from '@/views/music/songList/category';
// MV的地区管理
import Region from '@/views/music/mv/region';
// MV的类型管理
import Type from '@/views/music/mv/type';
// 用户管理
import user from '@/views/user/index';
// 菜单管理
import Module from '@/views/system/Module';
// 角色管理
import Role from '@/views/role/index';
// 系统环境变量
import Variable from '@/views/system/Variable';
// 权限管理
import Permission from '@/views/permission';
import blogList from '@/views/blog/index';
import Opinion from '@/views/website/Opinion';

import Account from '../components/account.vue';
import Message from '@/views/website/message.vue';
/**
 * 支付管理
 */
// 支付配置信息
import MachineConfig from '@/views/machine/MachineConfig';
// 支付配置
import Config from '@/views/pay/Config';
import Log from '@/views/website/log';
/**
 * 数据监控
 */
// 监控查询
import druidLogin from '@/views/druid/login';

import Comment from '@/views/website/comment';

// 图表界面
import statistics from '@/views/charts/statistics';

// 发表博客页面
import addBlog from '@/views/blog/addBlog';

import Mv from '@/views/music/mv';

// 网站配置
import WebsiteConfig from '@/views/website';
// 启用路由
Vue.use(Router);

// 导出路由
const router = new Router({
    routes: [{
        path: '/',
        name: '',
        component: login,
        hidden: true,
        meta: {
            requireAuth: false
        }
    }, {
      path: '/account',
      name: '',
      component: Account,
      hidden: true,
      meta: {
          requireAuth: false
      }
    },{
        path: '/login',
        name: '登录',
        component: login,
        hidden: true,
        meta: {
            requireAuth: false
        }
    }, {
        path: '/index',
        name: '首页',
        component: index,
        iconCls: 'el-icon-tickets',
        children: [{
            path: '/charts/statistics',
            name: '首页管理',
            component: statistics,
            meta: {
                requireAuth: true
            }
        }, {
            path: '/system/user',
            name: '用户管理',
            component: user,
            meta: {
                requireAuth: true
            }
        }, {
            path: '/system/Module',
            name: '菜单管理',
            component: Module,
            meta: {
                requireAuth: true
            }
        }, {
            path: '/system/Role',
            name: '角色管理',
            component: Role,
            meta: {
                requireAuth: true
            }
        }, {
            path: '/system/Variable',
            name: '系统环境变量',
            component: Variable,
            meta: {
                requireAuth: true
            }
        }, {
            path: '/system/Permission',
            name: '权限管理',
            component: Permission,
            meta: {
                requireAuth: true
            }
        }, {
            path: '/machine/MachineConfig',
            name: '支付配置信息',
            component: MachineConfig,
            meta: {
                requireAuth: true
            }
        }, {
            path: '/pay/Config',
            name: '支付配置',
            component: Config,
            meta: {
                requireAuth: true
            }
        }, {
            path: '/druid/login',
            name: '监控查询',
            component: druidLogin,
            meta: {
                requireAuth: true
            }
        }, {
            path: '/charts/statistics',
            name: '数据可视化',
            component: statistics,
            meta: {
                requireAuth: true
            }
        },{
            path: '/blog/addBlog',
            name: '发表博客',
            component: addBlog,
            meta:{title:'发表博客'},
        },{
          path: '/blog/list',
          name: '博客列表',
          component: blogList,
          meta:{title:'博客列表'},
        },{
            path: '/category/list',
            name: '分类管理',
            component: Category,
            meta: {
              requireAuth: true
            }
          },{
          path: '/label/list',
          name: '标签管理',
          component: Label,
          meta: {
            requireAuth: true
          }
        }, {
          path: '/picture/list',
          name: '相册管理',
          component: Picture,
          meta: {
            requireAuth: true
          }
        }, {
          path: '/picture/detail/:id',
          name: '相册照片详情',
          component: Detail,
          meta: {
            requireAuth: true
          }
        },{
          path: '/music/song',
          name: '音乐列表',
          component: Song,
          meta: {
            requireAuth: true
          }
        }, {
          path: '/music/singer',
          name: '歌手列表',
          component: Singer,
          meta: {
            requireAuth: true
          }
        }, {
          path: '/music/mv',
          name: 'MV列表',
          component: Mv,
          meta: {
            requireAuth: true
          },
        },{
            path: '/music/song/category',
            name: '歌曲分类',
            component: SongCategory,
            meta: {
              requireAuth: true
            },
          }, {
            path: '/music/mv/category',
            name: 'MV分类',
            component: MvCategory,
            meta: {
              requireAuth: true
            },
          },{
          path: '/music/songList',
          name: '歌单列表',
          component: SongList,
          meta: {
            requireAuth: true
          },
        }, {
          path: '/music/songList/category',
          name: '歌单分类',
          component: SongListCategory,
          meta: {
            requireAuth: true
          },
        },{
            path: '/music/mv/region',
            name: '地区管理',
            component: Region,
            meta: {
              requireAuth: true
            },
        },{
          path: '/music/mv/type',
          name: '类型管理',
          component: Type,
          meta: {
            requireAuth: true
          },
        },{
            path: '/website/config',
            name: '网站配置',
            component: WebsiteConfig,
            meta: {
              requireAuth: true
            },
        },{
          path: '/website/log',
          name: '日志信息',
          component: Log,
          meta: {
            requireAuth: true
          },
        },{
          path: '/website/comment',
          name: '评论管理',
          component: Comment,
          meta: {
            requireAuth: true
          },
        }, {
          path: '/website/opinion',
          name: '用户反馈',
          component: Opinion,
          meta: {
            requireAuth: true
          },
        }, {
          path: '/website/message',
          name: '留言管理',
          component: Message,
          meta: {
            requireAuth: true
          },
        }
      ]
    }]
})

// 导航守卫
// 使用 router.beforeEach 注册一个全局前置守卫，判断用户是否登陆
router.beforeEach((to, from, next) => {
  let accessToken = PcCookie.get(Key.accessTokenKey);
  if (to.path === '/login' || to.path === '/' || to.path === '/account') {
    if (accessToken === null || accessToken === '' || accessToken === undefined) {
      next();
    } else {
      next('/charts/statistics');
    }
  } else {
    if (accessToken === null || accessToken === '' || accessToken === undefined) {
      console.log('没有令牌，回到登录页面...');
      next('/login');
    } else {
      next();
    }
  }
});

export default router;
